import 'package:flutter/material.dart';

class TodoListviewItem extends StatelessWidget {
  TodoListviewItem({
    Key key,
    @required this.lable,
    this.time,
    this.color = Colors.transparent,
    this.margin = EdgeInsets.zero,
    this.onValueChanged,
  }) : super(key: key);

  final String lable;
  final String time;
  final EdgeInsets margin;
  final Color color;
  final ValueChanged<bool> onValueChanged;

  @override
  Widget build(BuildContext context) {
    return Container(
      margin: margin,
      padding: EdgeInsets.all(5),
      child: Row(
        children: <Widget>[
          Expanded(
            child: Text(
              lable,
              overflow: TextOverflow.ellipsis,
              style: TextStyle(fontSize: 14, fontWeight: FontWeight.bold),
            ),
          ),
          SizedBox(width: 20),
          _RadioButton(color: color, onValueChanged: onValueChanged),
        ],
      ),
    );
  }
}

class _RadioButton extends StatefulWidget {
  _RadioButton({
    Key key,
    @required this.color,
    this.onValueChanged,
  }) : super(key: key);

  final Color color;
  final ValueChanged<bool> onValueChanged;

  @override
  State<StatefulWidget> createState() => _RadioButtonState();
}

class _RadioButtonState extends State<_RadioButton> {
  bool isCheck = false;

  @override
  void initState() {
    super.initState();
  }

  void onPressed() {
    setState(() {
      isCheck = !isCheck;
    });
    if (widget.onValueChanged != null) {
      widget.onValueChanged(isCheck);
    }
  }

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      child: Icon(
        isCheck ? Icons.radio_button_checked : Icons.radio_button_unchecked,
        color: widget.color,
      ),
      onTap: onPressed,
    );
  }
}
